package com.atguigu.spzx.manage.service.Impl;

import com.atguigu.spzx.common.exception.GuiguException;
import com.atguigu.spzx.manage.mapper.CommunityMapper;
import com.atguigu.spzx.manage.service.CommunityService;
import com.atguigu.spzx.manage.utils.MenuHelper;
import com.atguigu.spzx.model.entity.system.Community;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class CommunityServiceImpl  implements CommunityService {

    @Autowired
    private CommunityMapper communityMapper;

    @Override
    public List<Community> findNodes() {
        //1 查询所有菜单，返回list集合
        List<Community> communityList = communityMapper.findAll();
        if (CollectionUtils.isEmpty(communityList)) {
            return null;
        }

        //2 调用工具类的方法，把返回list集合封装要求数据格式
        List<Community> treeList = MenuHelper.buildTree(communityList);
        return treeList;
    }


    @Override
    public void saveEvent(Community community) {
        communityMapper.saveCommunity(community);
        //新添加子菜单，把父菜单isHalf半开状态 1
//        updateParentIsHalf(community);

    }

    //新添加子菜单，把父菜单isHalf半开状态 1
/*    private void updateParentIsHalf(Community community) {
        //获取当前添加菜单的父菜单
        Community parentCommunity = communityMapper.selectParentCommunity(community.getParentId());
        if(parentCommunity != null) {
            //把父菜单isHalf半开状态 1
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId()) ;
            // 递归调用
            updateSysRoleMenu(parentMenu) ;
        }
    }*/


    @Override
    public void update(Community community) {
        communityMapper.updateCommunity(community);

    }

    @Override
    public void deleteById(Long id) {
        //根据当前菜单id，查询是否包含子菜单
        int count = communityMapper.selectCountById(id);

        //判断，count大于0，包含子菜单
        if (count > 0) {
            throw new GuiguException(ResultCodeEnum.NODE_ERROR);
        }

        //count等于0 ，直接删除
        communityMapper.deleteById(id);
    }

    //查看一级社区再查看二级社区
    @Override
    public List<Community> findCommunityList(Long id) {
        //1 根据id条件值进行查询，返回list集合
        // SELECT * FROM category WHERE parent_id=id
        List<Community> communityList = communityMapper.selectCommunityByParentId(id);

        //2 遍历返回list集合，
        // 判断每个分类是否有下一层分类，如果有设置 hasChildren = true
        if(!CollectionUtils.isEmpty(communityList)) {
            communityList.forEach(community -> {
                // 判断每个分类是否有下一层分类
                //SELECT count(*) FROM category WHERE parent_id=1
                int count = communityMapper.selectCountById(community.getId());
                if(count > 0) {//下一层分类
                    community.setHasChildren(true);
                } else {
                    community.setHasChildren(false);
                }
            });
        }
        return communityList;
//        //查询爹社区
//        List<Community> communityParentList=communityMapper.selectCommunityParent();
//        //查看儿子社区
//        communityParentList.forEach(communityParent -> {
//            Long parentId=communityParent.getId();
//            List<Community> communityChildrenList=communityMapper.selectCommunityChildren(parentId);
//            return communityChildrenList;
//        });
    }

//    @Override
//    public List<Community> findCommunityName(Long id) {
//        //1 根据id条件值进行查询，返回list集合
//        // SELECT * FROM category WHERE parent_id=id
//        List<Community> communityNameList = communityMapper.selectCommunityNameByParentId(id);
//
//
//        return communityNameList;
//    }
}
